#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
using namespace std;

struct STATUS {
	int MAP[4][4];
	int player; //1:W -1:B
	int step;
	STATUS(void *source, int player, int step): player(player), step(step) {
		memcpy(MAP, source, sizeof(MAP));
	}
	bool operator == (STATUS &rhs) {
		for (int i=0; i<4; i++) {
			for (int j=0; j<4; j++) {
				if (MAP[i][j] != rhs.MAP[i][j])
					return false;
			}
		}
		return true;
	}
};

int MAP[4][4]; //0:O 1:W -1:B

bool is_solve(int map[4][4]) {
	for (int i=0; i<4; i++) {
		bool flag = true;
		for (int j=1; j<4; j++) {
			if (map[i][j] != map[i][j-1]) {
				flag = false;
				break;
			}
		}
		if (flag) return true;
	}
	for (int j=0; j<4; j++) {
		bool flag = true;
		for (int i=1; i<4; i++) {
			if (map[i][j] != map[i-1][j]) {
				flag = false;
				break;
			}
		}
		if (flag) return true;
	}
	bool flag2 = true;
	for (int i=1; i<4; i++) {
		if (map[i][i] != map[i-1][i-1]) {
			flag2 = false;
			break;
		}
	}
	if (flag2) return true;
	flag2 = true;
	for (int i=1; i<4; i++) {
		if (map[i][4-i-1] != map[i-1][4-i]) {
			flag2 = false;
			break;
		}
	}
	if (flag2) return true;
	return false;
}

bool is_searched(vector<STATUS> &book, STATUS &s) {
	for (vector<STATUS>::iterator iter = book.begin();
			iter != book.end();
			iter++) {
				if (*iter == s) return true;
	}
	return false;
}

void print_map(int map[4][4]) {
	for (int i=0; i<4; i++) {
		for (int j=0; j<4; j++)
			printf("%d", map[i][j]);
		printf("\n");
	}
	printf("\n\n");
}

int BFS(int first) {
	queue<STATUS> q;
	STATUS f(MAP, first, 0);
	q.push(f);
	vector<STATUS> book;
	book.push_back(f);
	int cnt = 0;
	while (!q.empty() && cnt<1000) {
		STATUS s = q.front(); q.pop();
		cnt++;
		//print_map(s.MAP);
		if (is_solve(s.MAP)) {
			return s.step;
		}
		for (int i=0; i<4; i++) {
			for (int j=0; j<4; j++) {
				if (s.MAP[i][j]==0) {
					if (i-1>=0 && s.MAP[i-1][j]==s.player) {
						s.MAP[i][j] = s.MAP[i-1][j];
						s.MAP[i-1][j] = 0;
						STATUS tmp = STATUS(s.MAP, -s.player, s.step+1);
						if (!is_searched(book, tmp)) {
							q.push(tmp);
							book.push_back(tmp);
						}
						s.MAP[i-1][j] = s.MAP[i][j];
						s.MAP[i][j] = 0;
					}
					if (i+1<=3 && s.MAP[i+1][j]==s.player) {
						s.MAP[i][j] = s.MAP[i+1][j];
						s.MAP[i+1][j] = 0;
						STATUS tmp = STATUS(s.MAP, -s.player, s.step+1);
						if (!is_searched(book, tmp)) {
							q.push(tmp);
							book.push_back(tmp);
						}
						s.MAP[i+1][j] = s.MAP[i][j];
						s.MAP[i][j] = 0;
					}
					if (j-1>=0 && s.MAP[i][j-1]==s.player) {
						s.MAP[i][j] = s.MAP[i][j-1];
						s.MAP[i][j-1] = 0;
						STATUS tmp = STATUS(s.MAP, -s.player, s.step+1);
						if (!is_searched(book, tmp)) {
							q.push(tmp);
							book.push_back(tmp);
						}
						s.MAP[i][j-1] = s.MAP[i][j];
						s.MAP[i][j] = 0;
					}
					if (j+1<=3 && s.MAP[i][j+1]==s.player) {
						s.MAP[i][j] = s.MAP[i][j+1];
						s.MAP[i][j+1] = 0;
						STATUS tmp = STATUS(s.MAP, -s.player, s.step+1);
						if (!is_searched(book, tmp)) {
							q.push(tmp);
							book.push_back(tmp);
						}
						s.MAP[i][j+1] = s.MAP[i][j];
						s.MAP[i][j] = 0;
					}
				}
			}
		}
	}
	return 100000000;
}

void input() {
	for (int i=0; i<4; i++) {
		 char c[10];
		 gets(c);
		 for (int j=0; j<4; j++) {
			 if (c[j]=='O') MAP[i][j] = 0;
			 else if (c[j]=='W') MAP[i][j] = 1;
			 else if (c[j]=='B') MAP[i][j] = -1;
		}
	}

}



int main() {
	input();
	int w_first = BFS(1);
	int b_first = BFS(-1);
	int result = w_first<b_first?w_first:b_first;
	printf("%d\n", result);
	return 0;
}
